package com.jn.mvvm2.views;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.jn.mvvm2.R;
import com.jn.mvvm2.base.BaseActivity;
import com.jn.mvvm2.databinding.ActivityNewsBinding;
import com.jn.mvvm2.entity.News;
import com.jn.mvvm2.viewmodel.NewsViewModel;

public class NewsActivity extends BaseActivity<ActivityNewsBinding> {

    private NewsViewModel mNewsViewModel;
    private LiveData<News> mNewsLiveData;
    @Override
    public int getLayoutId() {
        return R.layout.activity_news;
    }

    @Override
    public void init() {
        mNewsViewModel = new ViewModelProvider(this).get(NewsViewModel.class);
        mNewsLiveData = mNewsViewModel.getLiveData();
        mNewsLiveData.observe(this, new Observer<News>() {
            @Override
            public void onChanged(News news) {
                News.ResultDTO.NewslistDTO data = news.getResult().getNewslist().get(0);
                mBinding.setNews(data);
                Glide.with(NewsActivity.this).load("https:" + data.getPicUrl()).into(mBinding.image1);
            }
        });
        mNewsViewModel.getData();
    }
}